<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;

use \think\Db;
use app\admin\model\News as NewsModel;//管理员模型
use app\admin\controller\Permissions;
class Lawyer extends Permissions
{
    /**
     * 律师列表
     * @return [type] [description]
     */
    public function index()
    {

        $name=trim($this->request->Post('name'));
        if($name !='') $where['name']= ['like','%' . $name . '%'];

        $lawyer = empty($where)?Db::name('lawyer')->order("id","desc")->paginate(10,false,['query'=>request()->Post()]): Db::name('lawyer')->where($where)->order("id","desc")->paginate(10,true,['query'=>request()->Post()]);
        $this->assign('lawyer',$lawyer);
		$this->assign('page',$lawyer->render());
        // var_dump($news);
        return $this->fetch();
    }

    /**
     * 添加新闻
     */
    public function add(){
        //判断是不是操作数据库  
        if($this->request->isPost()){

            $post=$this->request->Post();
            //验证  唯一规则： 表名，字段名，排除主键值，主键名
            
            // $validate = new \think\Validate([
                // ['type_id', 'require', '新闻类型必选!'],
                // ['type_sort', 'number', '排序格式不正确,请填数字'],
                // ['start_time', 'require', '发布时间必填!'],
                // ['die_time', 'require', '过期时间必填'],
 
            // ]);
            //验证部分数据合法性
            // if (!$validate->check($post)) {
                // $this->error('提交失败：' . $validate->getError());
            // }
            $id=$this->request->Post("id");
            
            if($id!=''){
                $old_img=Db::name("lawyer")->where('id',$id)->value('zhengshu');
            }
              // 获取表单上传文件 例如上传了001.jpg
            $file = Request()->file("img");
            if(!empty($file)){
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads/lawyer');
                if($info){
                    $img =$info->getSaveName();
                    @unlink($_SERVER['DOCUMENT_ROOT']."shanghai/public/uploads/lawyer/".$old_img);
                }else{
                    echo $file->getError();
                }
            }else{
                $img=$old_img;
            }
            $data=[
                    'name'=>$post['name'],
                    'post'=>$post['post'],
					'sex'=>$post['sex'],
                    'content'=>$post['content'],
                    'zhengshu'=>$img,
                    "create_time"=>date("Y-m-d H:i:s")
                ];
				// return $data;
            if($id==null){       
                $id=Db::name('lawyer')->insert($data);
                if($id==false) return $this->error('保存失败');   
            }else{
                $data1=[
                    'name'=>$post['name'],
                    'post'=>$post['post'],
					'sex'=>$post['sex'],
                    'content'=>$post['content'],
                    'zhengshu'=>$img,
                ];
				
                $res = Db::name('lawyer')->where('id',$id)->update($data1);
                if($res==false) return $this->error('修改失败');     
            }
            return $this->success("保存成功",'admin/lawyer/index');
        }elseif($this->request->has('id')){
            //获取对应数据id
            $id=$this->request->param("id");
            $lawyer=Db::name("lawyer")->where("id",$id)->find();
            // var_dump($friendly);
            $this->assign("lawyer",$lawyer);
            return $this->fetch();
        }else{
            return $this->fetch();
        }
    }


     /**
     * 新闻删除
     * @return [type] [description]
     */
    public function delete()
    {
        if($this->request->isAjax()) {
            $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            if(false == Db::name('lawyer')->where('id',$id)->delete()) {
                return 1;
            } else {
                return 0;
            }
        }
    }


    /**
     * 新闻类别
     * 
     */
    public  function news_type(){
        $type=Db::name("news_type")->order("id","desc")->paginate(10);
        $this->assign("type",$type);
        return $this->fetch("news/news_type");
    }

        /**
     * 添加新闻类别
     */
    public function type_add(){
        //实例化模型
        $model=new NewsModel();

        //判断是不是操作数据库  
        if($this->request->isPost()){

            $post=$this->request->Post();
            $id=$this->request->Post("id");
            if($id==''){       
                $id=Db::name('news_type')->insert($post);
                if($id==false) return $this->error('修改失败');   
            }else{
                $res =Db::name('news_type')->where('id',$id)->update($post,$id);
                if($res==false) return $this->error('修改失败');     
            }
            return $this->success('新闻保存成功','admin/news/news_type');
        }else{
            return $this->fetch();
        }
    }

    /**
     * 修改页面
     */
    public function type_edit(){
        //实例化模型
        // $model=new NewsModel();
        //获取对应数据id
        $id=$this->request->param("id");
        $type=Db::name("news_type")->where("id",$id)->find();
        // var_dump($friendly);
        $this->assign("type",$type);
        return $this->fetch("news/type_add");
    } 


         /**
     * 新闻类别删除
     * @return [type] [description]
     */
    public function type_delete()
    {
        if($this->request->isAjax()) {
            $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
            if(false == Db::name('news_type')->where('id',$id)->delete()) {
                return 1;
            } else {
                // addlog($id);//写入日志
                return 0;
            }
        }
    }

    
}
